home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / NameAndAddress / NameAndAddress.m < prev    next >
Encoding:
Text File  |  2000-10-06  |  3.9 KB  |  123 lines

  1. /*
  2.     File:       NameAndAddress.m
  3.  
  4.     Contains:   The private implementation for the NameAndAddress class.
  5.                 NameAndAddress is a cover for the functionality of the the NSHost class.
  6.                 NameAndAddress acts as a controller that allows hostname or IP address lookups.
  7.                 In this interface, the IPAddressField is used by the hostNameButton,
  8.                 and the hostnameField is used by the IPAddressButton. The interface
  9.                 disables a button if its corresponding field is empty.
  10.  
  11.     Written by: Guy @Werk
  12.  
  13.     Created:    June 1997
  14.  
  15.     Copyright:  (c)1997 by Apple Computer, Inc., all rights reserved.
  16.  
  17.     Change History (most recent first):
  18.                  June  2000 - Updated to Project Builder on DP4
  19.           March 1998 - HI changes - DG
  20.                  April 1997 -- Created for the initial Rhapsody training.
  21.  
  22.     You may incorporate this sample code into your applications without
  23.     restriction, though the sample code has been provided "AS IS" and the
  24.     responsibility for its operation is 100% yours.  However, what you are
  25.     not permitted to do is to redistribute the source as "DSC Sample Code"
  26.     after having made changes. If you're going to re-distribute the source,
  27.     we require that you make it clear in the source that the code was
  28.     descended from Apple Sample Code, but that you've made changes.
  29. */
  30.  
  31. #import "NameAndAddress.h"
  32.  
  33. @implementation NameAndAddress
  34.  
  35. // D E L E G A T A T I O N   A N D   N O T I F I C A T I O N
  36.  
  37. // NSApplication notification method
  38. // gets the host name and IP address for the local host
  39. // places the window onscreen
  40. - (void)applicationDidFinishLaunching:(NSNotification *)notification
  41. {
  42.     NSHost *host;
  43.  
  44.     host = [NSHost currentHost];
  45.     if (host) {
  46.         [hostNameField setStringValue:[host name]];
  47.         [self getIPAddress:nil];
  48.     }    
  49.     [[hostNameField window] makeKeyAndOrderFront:nil];
  50. }
  51.  
  52. // NSControl notification method
  53. // checks as each character is entered in the text field
  54. // if the result is an empty text field, disables the corresponding button
  55. - (void)controlTextDidChange:(NSNotification *)notification
  56. {
  57.     NSControl *textField;
  58.     NSButton *button;
  59.  
  60.     // get the field that is sending the notification
  61.     textField = [notification object];
  62.     // get the button that corresponds to the field
  63.     button = (textField == IPAddressField) ? hostNameButton : IPAddressButton;
  64.     // disable that button if the field is empty
  65.     [[textField stringValue] isEqualToString:@""] ? [button setEnabled:NO] : [button setEnabled:YES];
  66. }
  67.  
  68. // A C T I O N S
  69.  
  70. // gets a host name for the IP adress in the IPAddressField
  71. - (void)getHostName:(id)sender
  72. {
  73.     NSHost *host;
  74.     NSString *IPAddress;
  75.  
  76.     // get the IP address
  77.     IPAddress = [IPAddressField stringValue];
  78.     // get a host for the IP address
  79.     host = [NSHost hostWithAddress:IPAddress];
  80.  
  81.     // if there is no host for that IP address,
  82.     // empty the host name and disable the corresponding button
  83.     if (!host) {
  84.         [hostNameField setStringValue:@""];
  85.         [IPAddressButton setEnabled:NO];
  86.         NSBeep();
  87.         return;
  88.     }
  89.  
  90.     // get and display the host name
  91.     [hostNameField setStringValue:[host name]];
  92.     // enable the corresponding button
  93.     [IPAddressButton setEnabled:YES];
  94. }
  95.  
  96. // get an IP address for the host name in the hostNameField
  97. - (void)getIPAddress:(id)sender
  98. {
  99.     NSHost *host;
  100.     NSString *hostName;
  101.  
  102.     // get the host name
  103.     hostName = [hostNameField stringValue];
  104.     // get the host for the host name
  105.     host = [NSHost hostWithName:hostName];
  106.  
  107.     // if there is no host for that host name,
  108.     // empty the IP address and disable the corresponding button
  109.     if (!host) {
  110.         [IPAddressField setStringValue:@""];
  111.         [hostNameButton setEnabled:NO];
  112.         NSBeep();
  113.         return;
  114.     }
  115.  
  116.     // get and display the IP address
  117.     [IPAddressField setStringValue:[host address]];
  118.     // enable the corresponding button
  119.     [hostNameButton setEnabled:YES];
  120. }
  121.  
  122. @end
  123.